package gorm2sqlite

import (
	"gitee.com/z1gotool/z1err"
	"github.com/cloudquery/sqlite" // no cgo
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

// Gen new gorm DB
//
// dsn		z1sqlite.db/:memory:
func NewDB(dsn string, gormConfig gorm.Config) (db *gorm.DB, err error) {
	defer z1err.Handle(&err)

	dialector := sqlite.Open(dsn)

	if gormConfig.NamingStrategy == nil {
		gormConfig.NamingStrategy = schema.NamingStrategy{
			// TablePrefix:   "t_", // 表名前缀，`User` 的表名应该是 `t_users`
			SingularTable: true, // 使用单数表名，启用该选项，此时，`User` 的表名应该是 `t_user`
		}
	}

	dbTmp, err := gorm.Open(dialector, &gormConfig)
	z1err.Check(err)

	db = dbTmp

	return
}
